package utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.client.utils.URLEncodedUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import models.UploadLog;
import services.UploadLogService;
import vos.AppDataVo;

public class UrlUtils {
	public static boolean url(String requestUrl,AppDataVo vo,String pdfname){
		String json=JSON.toJSONString(vo);
        PrintWriter printWriter = null;  
        BufferedReader bufferedReader = null;  
        StringBuffer responseResult = new StringBuffer();
		HttpURLConnection httpURLConnection = null;  
		  try { 
	            URL realUrl = new URL(requestUrl);  
	            // 打开和URL之间的连接  
	            httpURLConnection = (HttpURLConnection) realUrl.openConnection();  
	            // 设置通用的请求属性  
	            httpURLConnection.setRequestProperty("accept", "*/*");
	            httpURLConnection.setRequestMethod("POST");
	            httpURLConnection.setRequestProperty("Accept-Charset", "UTF-8");
	            httpURLConnection.setRequestProperty("connection", "Keep-Alive");  
	            httpURLConnection.setRequestProperty("Content-Length", String  
	                    .valueOf(json.length()));  
	            httpURLConnection.setConnectTimeout(30000);
	            httpURLConnection.setReadTimeout(30000);
	            System.out.println(json.getBytes().length);
	            // 发送POST请求必须设置如下两行  
	            httpURLConnection.setDoOutput(true);  
	            httpURLConnection.setDoInput(true);  
	            // 获取URLConnection对象对应的输出流  
	            printWriter = new PrintWriter(httpURLConnection.getOutputStream());  
	            // 发送请求参数  
	            printWriter.write(json.toString());  
	            // flush输出流的缓冲  
	            printWriter.flush();  
	            // 根据ResponseCode判断连接是否成功  
	            int responseCode = httpURLConnection.getResponseCode();  
	            // 定义BufferedReader输入流来读取URL的ResponseData  
	            bufferedReader = new BufferedReader(new InputStreamReader(  
	                    httpURLConnection.getInputStream()));  
	            String line;  
	            while ((line = bufferedReader.readLine()) != null) {  
	                responseResult.append(line);  
	            } 
	            if (responseCode != 200) {  
	            	UploadLog log=new UploadLog(pdfname,999,"网络异常","",new Date(),responseResult.toString());
					//保存上传日志
					UploadLogService.saveLog(log);
					return false;
	            } else {  
					JSONObject result= JSON.parseObject(responseResult.toString());
					Integer state=(Integer)result.get("StateCode");
					String msg=(String)result.get("Msg");
					String value=(String)result.get("Value");
					UploadLog log=new UploadLog(pdfname,state,msg,value,new Date(),responseResult.toString());
					//保存上传日志
					UploadLogService.saveLog(log);
					if(state==1){
						return true;
					}
					else{
						return false;
					}
	            }  
	            
	        } catch (Exception e) {  
	           e.printStackTrace();
	           UploadLog log=new UploadLog(pdfname,990,"系统异常","",new Date(),responseResult.toString());
				//保存上传日志
				UploadLogService.saveLog(log);
	           return false;
	        } finally {  
	            httpURLConnection.disconnect();  
	            try {  
	                if (printWriter != null) {  
	                    printWriter.close();  
	                }  
	                if (bufferedReader != null) {  
	                    bufferedReader.close();  
	                }  
	            } catch (IOException ex) {  
	                ex.printStackTrace();  
	            }  
	  
	        }
	}
}
